﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using GtsServer.Common;
using System.Data;

namespace GtsServer.DAL
{
    public class FuncPrizeParmsDAL
    {
        public FuncPrizeParmsDAL()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from FuncPrizeParms");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public DataSet GetListByPrizeID(int prizeID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select fp.ID,fp.Name,fp.PrizeID,fp.EventMouldID,ecm.Name EventMouldName,fp.GainPrizeID,fp.MaxValue,fp.MinValue,fp.FloatRatio,fp.PlaceTime,fp.FuncChance,fp.FuncDayAstrict, ");
            strSql.Append("case isnull(fp.PlacePrizeID,0) when 1 then '额外金币' when 2 then '比赛积分加成' when 3 then '小图案倍率' when 4 then '额外彩蛋' else '' end  PlacePrizeName, ");
            strSql.Append("fp.PlacePrizeID,fp.Param1,fp.Param2,p.Name GainPrizeName FROM FuncPrizeParms fp ");
            strSql.Append("left join EventChanceMould ecm on fp.EventMouldID = ecm.ID ");
            strSql.Append("left join Prize p on fp.GainPrizeID=p.ID ");
            strSql.Append(" where fp.prizeID=" + prizeID);
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(GtsServer.Model.FuncPrizeParms model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into FuncPrizeParms(");
            strSql.Append("Name,PrizeID,EventMouldID,GainPrizeID,MaxValue,MinValue,FloatRatio,PlaceTime,PlacePrizeID,Param1,Param2,FuncChance,FuncDayAstrict)");
            strSql.Append(" values (");
            strSql.Append("@Name,@PrizeID,@EventMouldID,@GainPrizeID,@MaxValue,@MinValue,@FloatRatio,@PlaceTime,@PlacePrizeID,@Param1,@Param2,@FuncChance,@FuncDayAstrict)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@PrizeID", SqlDbType.Int,4),
					new SqlParameter("@EventMouldID", SqlDbType.Int,4),
					new SqlParameter("@GainPrizeID", SqlDbType.Int,4),
					new SqlParameter("@MaxValue", SqlDbType.Int,4),
					new SqlParameter("@MinValue", SqlDbType.Int,4),
					new SqlParameter("@FloatRatio", SqlDbType.Int,4),
					new SqlParameter("@PlaceTime", SqlDbType.Int,4),
					new SqlParameter("@PlacePrizeID", SqlDbType.Int,4),
					new SqlParameter("@Param1", SqlDbType.Int,4),
					new SqlParameter("@Param2", SqlDbType.Int,4),
					new SqlParameter("@FuncChance", SqlDbType.Int,4),
					new SqlParameter("@FuncDayAstrict", SqlDbType.Int,4)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.PrizeID;
            parameters[2].Value = model.EventMouldID;
            parameters[3].Value = model.GainPrizeID;
            parameters[4].Value = model.MaxValue;
            parameters[5].Value = model.MinValue;
            parameters[6].Value = model.FloatRatio;
            parameters[7].Value = model.PlaceTime;
            parameters[8].Value = model.PlacePrizeID;
            parameters[9].Value = model.Param1;
            parameters[10].Value = model.Param2;
            parameters[11].Value = model.FuncChance;
            parameters[12].Value = model.FuncDayAstrict;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(GtsServer.Model.FuncPrizeParms model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update FuncPrizeParms set ");
            strSql.Append("Name=@Name,");
            strSql.Append("PrizeID=@PrizeID,");
            strSql.Append("EventMouldID=@EventMouldID,");
            strSql.Append("GainPrizeID=@GainPrizeID,");
            strSql.Append("MaxValue=@MaxValue,");
            strSql.Append("MinValue=@MinValue,");
            strSql.Append("FloatRatio=@FloatRatio,");
            strSql.Append("PlaceTime=@PlaceTime,");
            strSql.Append("PlacePrizeID=@PlacePrizeID,");
            strSql.Append("Param1=@Param1,");
            strSql.Append("Param2=@Param2,FuncChance=@FuncChance,FuncDayAstrict=@FuncDayAstrict");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@PrizeID", SqlDbType.Int,4),
					new SqlParameter("@EventMouldID", SqlDbType.Int,4),
					new SqlParameter("@GainPrizeID", SqlDbType.Int,4),
					new SqlParameter("@MaxValue", SqlDbType.Int,4),
					new SqlParameter("@MinValue", SqlDbType.Int,4),
					new SqlParameter("@FloatRatio", SqlDbType.Int,4),
					new SqlParameter("@PlaceTime", SqlDbType.Int,4),
					new SqlParameter("@PlacePrizeID", SqlDbType.Int,4),
					new SqlParameter("@Param1", SqlDbType.Int,4),
					new SqlParameter("@Param2", SqlDbType.Int,4),
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@FuncChance", SqlDbType.Int,4),
					new SqlParameter("@FuncDayAstrict", SqlDbType.Int,4)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.PrizeID;
            parameters[2].Value = model.EventMouldID;
            parameters[3].Value = model.GainPrizeID;
            parameters[4].Value = model.MaxValue;
            parameters[5].Value = model.MinValue;
            parameters[6].Value = model.FloatRatio;
            parameters[7].Value = model.PlaceTime;
            parameters[8].Value = model.PlacePrizeID;
            parameters[9].Value = model.Param1;
            parameters[10].Value = model.Param2;
            parameters[11].Value = model.ID;
            parameters[12].Value = model.FuncChance;
            parameters[13].Value = model.FuncDayAstrict;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一个道具的所有参数数据
        /// </summary>
        public bool DeleteByPrizeID(int prizeID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from FuncPrizeParms ");
            strSql.Append(" where PrizeID=@prizeID");
            SqlParameter[] parameters = {
					new SqlParameter("@prizeID", SqlDbType.Int,4)
};
            parameters[0].Value = prizeID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from FuncPrizeParms ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from FuncPrizeParms ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public GtsServer.Model.FuncPrizeParms GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,Name,PrizeID,EventMouldID,GainPrizeID,MaxValue,MinValue,FloatRatio,PlaceTime,PlacePrizeID,Param1,Param2,FuncChance,FuncDayAstrict from FuncPrizeParms ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            GtsServer.Model.FuncPrizeParms model = new GtsServer.Model.FuncPrizeParms();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Name"] != null && ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["PrizeID"] != null && ds.Tables[0].Rows[0]["PrizeID"].ToString() != "")
                {
                    model.PrizeID = int.Parse(ds.Tables[0].Rows[0]["PrizeID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EventMouldID"] != null && ds.Tables[0].Rows[0]["EventMouldID"].ToString() != "")
                {
                    model.EventMouldID = int.Parse(ds.Tables[0].Rows[0]["EventMouldID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["GainPrizeID"] != null && ds.Tables[0].Rows[0]["GainPrizeID"].ToString() != "")
                {
                    model.GainPrizeID = int.Parse(ds.Tables[0].Rows[0]["GainPrizeID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MaxValue"] != null && ds.Tables[0].Rows[0]["MaxValue"].ToString() != "")
                {
                    model.MaxValue = int.Parse(ds.Tables[0].Rows[0]["MaxValue"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MinValue"] != null && ds.Tables[0].Rows[0]["MinValue"].ToString() != "")
                {
                    model.MinValue = int.Parse(ds.Tables[0].Rows[0]["MinValue"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FloatRatio"] != null && ds.Tables[0].Rows[0]["FloatRatio"].ToString() != "")
                {
                    model.FloatRatio = int.Parse(ds.Tables[0].Rows[0]["FloatRatio"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PlaceTime"] != null && ds.Tables[0].Rows[0]["PlaceTime"].ToString() != "")
                {
                    model.PlaceTime = int.Parse(ds.Tables[0].Rows[0]["PlaceTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PlacePrizeID"] != null && ds.Tables[0].Rows[0]["PlacePrizeID"].ToString() != "")
                {
                    model.PlacePrizeID = int.Parse(ds.Tables[0].Rows[0]["PlacePrizeID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Param1"] != null && ds.Tables[0].Rows[0]["Param1"].ToString() != "")
                {
                    model.Param1 = int.Parse(ds.Tables[0].Rows[0]["Param1"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Param2"] != null && ds.Tables[0].Rows[0]["Param2"].ToString() != "")
                {
                    model.Param2 = int.Parse(ds.Tables[0].Rows[0]["Param2"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,Name,PrizeID,EventMouldID,GainPrizeID,MaxValue,MinValue,FloatRatio,PlaceTime,PlacePrizeID,Param1,Param2,FuncChance,FuncDayAstrict ");
            strSql.Append(" FROM FuncPrizeParms ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,Name,PrizeID,EventMouldID,GainPrizeID,MaxValue,MinValue,FloatRatio,PlaceTime,PlacePrizeID,Param1,Param2,FuncChance,FuncDayAstrict ");
            strSql.Append(" FROM FuncPrizeParms ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "FuncPrizeParms";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}
